﻿using System;
using Microsoft.Xna.Framework;
using WindowsGame.Common.Data;
using WindowsGame.Common.Library;
using WindowsGame.Common.Interfaces;
using WindowsGame.Common.Static;

namespace WindowsGame.Common.Screens
{
	public class InitScreen : BaseScreen, IScreen
	{
		private Boolean join;

		public override void Initialize()
		{
			base.Initialize();
			CurrScreen = ScreenType.Init;

			Single wide = (Constants.SCREEN_WIDE - Assets.SteveProTexture.Width)/2.0f;
			Single high = (Constants.SCREEN_HIGH - Assets.SteveProTexture.Height)/2.0f;

			BannerPosition = new Vector2(wide, high);
			join = false;
		}

		public override void LoadContent()
		{
			base.LoadContent();
			MyGame.Manager.ThreadManager.LoadContentAsync();
		}

		public ScreenType Update(GameTime gameTime)
		{
			UpdateTimer(gameTime);

			// Do not attempt to progress until join.
			join = MyGame.Manager.ThreadManager.Join(1);
			if (!join)
			{
				return ScreenType.Init;
			}

			NextScreen = Update(ScreenType.Init);
			if (CurrScreen != NextScreen)
			{
				return NextScreen;
			}

			NextScreen = GetNextScreen();
			if (Timer > BaseData.SplashDelay)
			{
				return NextScreen;
			}

			Boolean start = MyGame.Manager.InputManager.Start();
			return start ? NextScreen : ScreenType.Init;
		}

		public override void Draw()
		{
			Engine.SpriteBatch.Draw(Assets.SteveProTexture, BannerPosition, Color.White);
			if (join)
			{
				String text = MyGame.Manager.TextManager.BuildVersion;
				if (!String.IsNullOrEmpty(text))
				{
					Engine.SpriteBatch.DrawString(Assets.EmulogicFont, text, Vector2.Zero, Color.White);
				}
			}
		}

		private static ScreenType GetNextScreen()
		{
			ScreenType screenType = MyGame.Manager.ConfigManager.ConfigData.ScreenType;
			if (ScreenType.Splash == screenType || ScreenType.Init == screenType)
			{
				screenType = ScreenType.Demo;
			}

			return screenType;
		}

	}
}